package Web;

import Bean.User;
import service.UserService;
import service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jianjun
 * @Describe
 * @creat 2021-12-13-20:05
 */
public class LoginServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求的参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //2.验证用户名密码是否存在
        if (userService.login(new User(null, username, password, null)) != null) {
            //存在，跳转登录成功页面
            request.getRequestDispatcher("/pages/user/login_success.jsp").forward(request,response);
        }else{
            //表单错误，参数回显
            request.setAttribute("msg","用户名或密码错误");
            request.setAttribute("username",username);
            //不存在，跳回登录界面
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request,response);
        }
    }
}
